<?php
declare (strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Cache;
use think\facade\Db;

require_once './sdk/WxpayAPI/lib/WxSend.Api.php';

/**提用户送达
 * Class AutomaticDelivery
 * @package app\command
 */
class AutomaticRemindUser extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('AutomaticRemindUser')
            ->setDescription('the AutomaticRemindUser command');
    }

    protected function execute(Input $input, Output $output)
    {
        try {
            $redis = Cache::store('redis')->handler();
            $sends = $redis->rPop('arrive');//从redis中取出来
            if (!empty($sends)) {
                $send_ids = explode(',', $sends);
                // 自动收货(系统评价)
                $page = 1;
                $limit = 50;
                while (true) {
                    $ids = Db::table('eb_user')->where('id', 'in', $send_ids)->field('openid')->page($page, $limit)->select()->toArray();
                    if (!$ids) break;
                    $openid_ids = array_column($ids, 'openid');
//                    file_put_contents('/tmp/b.txt', json_encode($openid_ids), FILE_APPEND);
                    foreach ($openid_ids as $key => $value) {
                        $send = new \WxSendApi();
                        $ds = $send->send_Template_arrive($value);
                        json_decode(file_put_contents('/tmp/user.txt', json_encode($ds), FILE_APPEND),true);
                    }
                    $page++;
                }
            }
        } catch (\Exception $e) {
            exit;
        }
    }
}
